package com.shiloop.sensors;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shiloop.sensors.dummy.DummyContent;

/**
 * A fragment representing a single Item detail screen.
 * This fragment is either contained in a {@link ItemListActivity}
 * in two-pane mode (on tablets) or a {@link ItemDetailActivity}
 * on handsets.
 */
public class ItemDetailFragment extends Fragment implements SensorEventListener {
    /**
     * The fragment argument representing the item ID that this fragment
     * represents.
     */
    public static final String ARG_ITEM_ID = "item_id";

    /**
     * The dummy content this fragment is presenting.
     */
//    private DummyContent.DummyItem mItem;


    /**
     * Sensor detail text
     */
    private TextView mSensorNameText;
    private TextView mSensorVersionText;
    private TextView mSensorVendorText;
    private TextView mSensorValueText;
    private TextView mSensorTitleText;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public ItemDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        if (getArguments().containsKey(ARG_ITEM_ID)) {
//            // Load the dummy content specified by the fragment
//            // arguments. In a real-world scenario, use a Loader
//            // to load content from a content provider.
//            mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
//        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_item_detail,
                null);

        if (null != rootView) {
            mSensorNameText = (TextView) rootView.findViewById(R.id.sensor_name_value);
            mSensorVersionText = (TextView) rootView.findViewById(R.id.sensor_version_value);
            mSensorVendorText = (TextView) rootView.findViewById(R.id.sensor_vendor_value);
            mSensorValueText = (TextView) rootView.findViewById(R.id.sensor_current_value);
            mSensorTitleText = (TextView) rootView.findViewById(R.id.title_of_sensor);
        }

        return rootView;
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        float[] value = event.values;
        Sensor sensor = event.sensor;
        if (mSensorNameText != null) mSensorNameText.setText(sensor.getName());
        if (mSensorVersionText != null) mSensorVersionText.setText("" + sensor.getVersion());
        if (mSensorVendorText != null) mSensorVendorText.setText(sensor.getVendor());
        StringBuilder sb = new StringBuilder();
        for (float v : value) {
            sb.append(v);
            sb.append("\n");
        }
        if (mSensorValueText != null) mSensorValueText.setText(sb.toString());
        if (mSensorTitleText != null) {
            mSensorTitleText.setText(getTitle(sensor.getType()));
        }
    }

    public String getTitle(int type) {
        switch (type) {
            case Sensor.TYPE_ACCELEROMETER:
                return getString(R.string.ACCELEROMETER);
            case Sensor.TYPE_AMBIENT_TEMPERATURE:
                return getString(R.string.AMBIENT_TEMPERATURE);
            case Sensor.TYPE_GAME_ROTATION_VECTOR:
                return getString(R.string.GAME_ROTATION_VECTOR);
            case Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR:
                return getString(R.string.GEOMAGNETIC_ROTATION_VECTOR);
            case Sensor.TYPE_ROTATION_VECTOR:
                return getString(R.string.ROTATION_VECTOR);
            case Sensor.TYPE_GRAVITY:
                return getString(R.string.GRAVITY);
            case Sensor.TYPE_GYROSCOPE:
                return getString(R.string.GYROSCOPE);
            case Sensor.TYPE_GYROSCOPE_UNCALIBRATED:
                return getString(R.string.GYROSCOPE_UNCALIBRATED);
            case Sensor.TYPE_HEART_RATE:
                return getString(R.string.HEART_RATE);
            case Sensor.TYPE_STEP_COUNTER:
                return getString(R.string.STEP_COUNTER);
            case Sensor.TYPE_STEP_DETECTOR:
                return getString(R.string.STEP_DETECTOR);
            case Sensor.TYPE_LINEAR_ACCELERATION:
                return getString(R.string.LINEAR_ACCELERATION);
            case Sensor.TYPE_MAGNETIC_FIELD:
                return getString(R.string.MAGNETIC_FIELD);
            case Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED:
                return getString(R.string.MAGNETIC_FIELD_UNCALIBRATED);
            case Sensor.TYPE_PRESSURE:
                return getString(R.string.PRESSURE);
            case Sensor.TYPE_PROXIMITY:
                return getString(R.string.PROXIMITY);
            case Sensor.TYPE_RELATIVE_HUMIDITY:
                return getString(R.string.RELATIVE_HUMIDITY);
            case Sensor.TYPE_SIGNIFICANT_MOTION:
                return getString(R.string.SIGNIFICANT_MOTION);
            case Sensor.TYPE_ORIENTATION:
                return getString(R.string.ORIENTATION);
            case Sensor.TYPE_LIGHT:
                return getString(R.string.LIGHT);
            default:
                return "";
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
